package cn.anc.aonicardv.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.MediaStore;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by yangdai on 2017/8/12.
 */

public class ActivityUtils {

    private static List<Activity> activities = new ArrayList<>();
    public static final int CHOOSE_VIDEO_REQUEST_CODE = 10086;
    public static final int CHOOSE_AUDIO_REQUEST_CODE = 10000;

    public static boolean isTop(Activity activity) {
        ActivityManager manager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = manager.getRunningTasks(1);
        if (tasks != null && !tasks.isEmpty()) {
            ActivityManager.RunningTaskInfo taskInfo = tasks.get(0);
            ComponentName componentName = taskInfo.topActivity;
            if (componentName.getClassName().equals(activity.getComponentName().getClassName())) {
                return true;
            }
        }
        return false;
    }

    public static Activity getActivity() {
        Class activityThreadClass = null;
        try {
            activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field pausedField = activityRecordClass.getDeclaredField("paused");
                pausedField.setAccessible(true);
                if (!pausedField.getBoolean(activityRecord)) {
                    Field activityField = activityRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    Activity activity = (Activity) activityField.get(activityRecord);
                    return activity;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void jumpActivity(Context context, Class c) {
        Intent intent = new Intent(context, c);
        context.startActivity(intent);
    }

    public static void jumpActivity(Context context, Class c, String key, String value) {
        Intent intent = new Intent(context, c);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void jumpActivity(Context context, Class c, String[] parameters) {
        Intent intent = new Intent(context, c);
        int i = 0;
        while (i < parameters.length) {
            String key = parameters[i];
            i++;
            String value = parameters[i];
            i++;
            intent.putExtra(key, value);

        }
        context.startActivity(intent);
    }

    public static void jumpActivity(Context context, Class c, String key, Serializable value) {
        Intent intent = new Intent(context, c);
        intent.putExtra(key, value);
        context.startActivity(intent);
    }

    public static void jumpActivity(Context context, Class c, String key, Serializable value, String key2, boolean value2) {
        Intent intent = new Intent(context, c);
        intent.putExtra(key, value);
        intent.putExtra(key2, value2);
        context.startActivity(intent);
    }

    public static void jumpActivityForResult(Activity context, Class c, String[] parameters, int requestCode) {
        Intent intent = new Intent(context, c);
        int i = 0;
        if (parameters != null) {
            while (i < parameters.length) {
                String key = parameters[i];
                i++;
                String value = parameters[i];
                i++;
                intent.putExtra(key, value);
            }
        }
        context.startActivityForResult(intent, requestCode);
    }

    public static void jumpActivityForResult(Activity context, Class c, String key, int value, int requestCode) {
        Intent intent = new Intent(context, c);
        intent.putExtra(key, value);
        context.startActivityForResult(intent, requestCode);
    }

    public static void jumpActivityForResult(Activity context, Class c, String key, String value, int requestCode) {
        Intent intent = new Intent(context, c);
        intent.putExtra(key, value);
        context.startActivityForResult(intent, requestCode);
    }

    public static void addActivity(Activity activity) {
        boolean isContain = false;
        for (int i = 0; i < activities.size(); i++) {
            Activity activity1 = activities.get(i);
            if (activity1 == activity) {
                isContain = true;
                break;
            }
        }
        if (!isContain)
            activities.add(activity);
    }

    public static void removeActivity(Activity activity){
        for (int i = 0; i < activities.size(); i++) {
            Activity activity1 = activities.get(i);
            if (activity1 == activity) {
                activities.remove(activity1);
            }
        }
    }

    public static void finishActivityByName(Class c) {
        for (int i = 0; i < activities.size(); i++) {
            Activity activity1 = activities.get(i);
            if (activity1.getClass() == c) {
                activity1.finish();
            }
        }
    }

    public static void finishAll() {
        for (int i = 0; i < activities.size(); i++) {
            Activity activity1 = activities.get(i);
            activity1.finish();
        }
    }

    public static void chooseVideoFromLocal(Activity context,int requestCode){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("video/*");
        context.startActivityForResult(intent,requestCode);
    }

    public static void chooseAudioFromLocal(Activity context,int requestCode){
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/*");
        context.startActivityForResult(intent,requestCode);
    }

}
